package com.wedo.mall.activity.goods;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.widget.EBadgeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wedo.mall.R;
import com.wedo.mall.activity.MainActivity;
import com.wedo.mall.activity.login.WLogin;
import com.wedo.mall.activity.main.SearchActivity;
import com.wedo.mall.adapter.GoodsAdapter;
import com.wedo.mall.entity.Goods;
import com.wedo.mall.entity.GoodsCategory;
import com.wedo.mall.entity.Shopping;
import com.wedo.mall.ptr.Pull2Refresh;
import com.wedo.mall.util.WUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * 推荐商品或者商品列表UI
 * 1、首页-推荐商品
 * 2、首页商品类别-查看分类商品
 * Created by e on 2017/7/15.
 */

public class GoodsActivity extends EActivity {

    public static final String EXTRA_URL = "url";
    public static final String EXTRA_TITLE = "title";
    public static final String EXTRA_CATEGORY = "category";

    @BindView(R.id.iv_car) ImageView iv_car;
    @BindView(R.id.tv_title) TextView tv_title;
    @BindView(R.id.tv_empty) TextView tv_empty;
    @BindView(R.id.rv_list) XRecyclerView rv_list;
    @BindViews({R.id.tv_sort_comment,R.id.tv_sort_sale,R.id.tv_sort_price,R.id.tv_sort_auth}) List<TextView> tvs;

    private SparseArray<String> sorts = new SparseArray<>(4);

    private Pull2Refresh<Goods> ptr;
    public EBadgeView eBadgeView;


    public static void toRecommend(EActivity activity){
        String title = activity.getString(R.string.index_recommend_goods);
        toGoodsActivity(activity,EApi.GOODS_RECOMMEND,title,null);
    }

    public static void toCategory(EActivity activity,GoodsCategory category){
        String title = activity.getString(R.string.goods_list);
        toGoodsActivity(activity,EApi.GOODS_LIST,title,category);
    }

    public static void toGoodsActivity(EActivity activity,String url,String title,GoodsCategory category){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_URL,url);
        bundle.putString(EXTRA_TITLE,title);
        bundle.putParcelable(EXTRA_CATEGORY,category);
        activity.toActivity(GoodsActivity.class,bundle);
    }

    @Override
    protected int layout() {
        return R.layout.activity_goods;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        Intent intent = getIntent();

        String url = intent.getStringExtra(EXTRA_URL);
        String title = intent.getStringExtra(EXTRA_TITLE);
        GoodsCategory category = intent.getParcelableExtra(EXTRA_CATEGORY);

        tv_title.setText(title);

        eBadgeView = new EBadgeView(this);
        eBadgeView.setTargetView(iv_car);
        eBadgeView.setBadgeMargin(10,0,0,0);

        rv_list.setHasFixedSize(true);
        rv_list.setLayoutManager(new GridLayoutManager(this,2));

        ptr = new Pull2Refresh<>();

        GoodsAdapter adapter = new GoodsAdapter(this,null);
        adapter.listener(this);
        adapter.setOnShoppingListener(new GoodsAdapter.OnShoppingListener() {
            @Override
            public void onAdd2Car(int number) {
                int count = eBadgeView.getBadgeCount()+number;
                eBadgeView.setBadgeCount(count);
            }
        });

        /**商品分类*/
        if (category !=null){
            ptr.param("category_id",category.id);
        }

        ptr.widget(rv_list).adapter(adapter).url(url).clazz(Goods.class).emptyWidget(tv_empty).done();

        sorts.put(R.id.tv_sort_comment,"comment_sort");
        sorts.put(R.id.tv_sort_sale,"sale_sort");
        sorts.put(R.id.tv_sort_price,"price_sort");
        sorts.put(R.id.tv_sort_auth,"is_auth");

        shoppingCar();

    }

    @OnClick(R.id.iv_car)
    protected void onShoppingCarClick(){
        MainActivity.toShopping(this);
    }

    @OnClick(R.id.iv_search)
    protected void onSearchClick(){
        SearchActivity.toSearchGoods(this);
    }

    @OnClick(R.id.iv_backward)
    protected void onBackClick(){
        defaultFinish();
    }

    @OnClick({R.id.tv_sort_comment,R.id.tv_sort_sale,R.id.tv_sort_price,R.id.tv_sort_auth})
    protected void onSortClick(View view){
//        int id = view.getId();
//        String key = sorts.get(id);
//        view.setSelected(!view.isSelected());
////        String value = view.isSelected()?"1":"0";
////        ptr.param(key,value).refresh();
//
//        if(view.isSelected()) {
//            ptr.param(key,"1").refresh();
//        }else {
//            ptr.removeParam(key).refresh();
//        }
        for (TextView tv:tvs){
            if(view.getId() != tv.getId()) {
                /**未选*/
                String key = sorts.get(tv.getId());
                ptr.removeParam(key);
                tv.setSelected(false);
            } else {
                /**选中*/
                view.setSelected(!view.isSelected());
                String key = sorts.get(view.getId());
                if (view.isSelected()) {
                    ptr.param(key, "1").refresh();
                } else {
                    ptr.removeParam(key).refresh();
                }
            }
        }
    }

    private void shoppingCar(){
        if(!WLogin.logged(this)) {
            return;
        }

        EClient.with().post(EApi.SHOPPING_CAR, new EObserver() {
            @Override
            public void successful(EResponse response) {
                List<Shopping> list = response.list(Shopping.class);
                int number = WUtils.goodsNumber(list);
                eBadgeView.setBadgeCount(number);
            }

            @Override
            public void failure(String msg) {
                eBadgeView.setBadgeCount(0);
            }
        });
    }
}
